package com.coded2.infra.comum;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import com.coded2.infra.util.Reflexao;

public class UtilBean {

	private UtilBean() {
		super();
	}

	public static final String ACTION_LIST = "list";
	public static final String ACTION_CREATE = "create";
	public static final String ACTION_EDIT = "edit";
	public static final String ACTION_SAVE = "save";

	public static <MODEL>List<SelectItem>convertToSelectItem(Collection<MODEL>colecao,String label, String value) throws Exception{
		List<SelectItem> retorno = new ArrayList<SelectItem>();
		for (MODEL obj : colecao) {
			retorno.add(new SelectItem(Reflexao.getPropertyValue(obj, value),Reflexao.getPropertyValue(obj, label).toString()));
		}
		return retorno;
	}

	public static void addErrorMessage(String errorMessage) {
		FacesMessage msg = new FacesMessage(errorMessage);
		msg.setSeverity(FacesMessage.SEVERITY_ERROR);
		FacesContext.getCurrentInstance().addMessage(null, msg);
		
	}

	public static void addInfoMessage(String errorMessage) {
		FacesMessage msg = new FacesMessage(errorMessage);
		msg.setSeverity(FacesMessage.SEVERITY_INFO);
		FacesContext.getCurrentInstance().addMessage(null, msg);
		
	}
}
